home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / gui / precog2_1.lha / Precognition2_1 / src / src.lha / Library / AmigaMem.c next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  1.4 KB  |  96 lines

  1. #include "AmigaMem.h"
  2. #include <exec/memory.h>
  3. #include <string.h>
  4. #ifndef __GNUC__
  5. #include <clib/exec_protos.h>
  6. #endif
  7. #ifdef __GNUC__
  8. #include <proto/exec.h>
  9. #endif
  10. #ifdef __SASC
  11. #include <proto/exec.h>
  12. #endif
  13. #include "minmax.h"
  14.  
  15. void* Amalloc( ULONG size )
  16. {
  17.    ULONG *base;
  18.  
  19.    if( base = AllocMem( size+4, MEMF_CLEAR | MEMF_PUBLIC ) )
  20.    {
  21.       base[0] = size + 4;
  22.       return (void *)&base[1];
  23.    }
  24.    else
  25.       return NULL;
  26. }
  27.  
  28.  
  29. void* Acalloc( ULONG size, ULONG number )
  30. {
  31.    return Amalloc( size * number );
  32. }
  33.  
  34. char* Astrdup( const char *string )
  35. {
  36.    char* dup;
  37.    int len;
  38.  
  39.    if( string == NULL ) return NULL;
  40.  
  41.    len = strlen( string );
  42.  
  43.    if( dup = Amalloc( len + 1 ) )
  44.    {
  45.       strcpy( dup, string );
  46.    }
  47.    return dup;
  48. }
  49.  
  50.  
  51. void Afree( void* mem )
  52. {
  53.    ULONG *base;
  54.  
  55.    if( mem )
  56.    {
  57.       base = ( (ULONG *)mem ) -1;
  58.  
  59.       FreeMem( base, base[0] );
  60.    }
  61. }
  62.  
  63.  
  64. ULONG Asizeof( void* buf )
  65. {
  66.    ULONG *base;
  67.  
  68.    if( buf )
  69.    {
  70.       base = ( (ULONG *)buf ) -1;
  71.       return base[0];
  72.    }
  73.    else
  74.       return 0;
  75. }
  76.  
  77. void *Arealloc( void *oldbuf, ULONG newsize )
  78. {
  79.    void *newbuf = Amalloc( newsize );
  80.    ULONG minsize, oldsize;
  81.  
  82.    if( newbuf )
  83.    {
  84.       oldsize = ( oldbuf ) ? Asizeof( oldbuf ) : 0;
  85.       minsize = MIN( oldsize, newsize );
  86.  
  87.       memcpy( newbuf, oldbuf, minsize );
  88.       Afree( oldbuf );
  89.  
  90.       return newbuf;
  91.    }
  92.    else
  93.       return NULL;
  94.  
  95. }
  96.